import Message from 'tdesign-miniprogram/message/index'
import { login } from '../../api/login'

Page({
    data: {
        formType: 0,
        codeTime: 6,
        codeText: '获取验证码',
        isCodeLoading: false,
        isView: false,
        input1: '',
        input2: '',
        valid: true,
        rules1: {
            input1: [
                // required 规则必须放在 pattern 之前
                { required: true, message: '请输入手机号' },
                { pattern: '^1[3-9]\\d{9}$', message: '手机号格式有误，请重新输入' },
            ],
            input2: [
                { required: true, message: '请输入验证码' },
                { pattern: '^\\d{6}$', message: '验证码格式有误，请重新输入' },
            ],
        },
        rules2: {
            input1: [
                { required: true, message: '请输入账号' },
                { pattern: '^\\w{4,16}$', message: '账号格式有误，请重新输入' },
            ],
            input2: [
                { required: true, message: '请输入密码' },
                { pattern: '^\\w{4,16}$', message: '密码格式有误，请重新输入' },
            ],
        },
        message1: '',
        message2: '',
    },
    changeFormType() {
        this.setData({
            input1: '',
            input2: '',
            message1: '',
            message2: '',
            formType: this.data.formType === 0 ? 1 : 0,
        })
        if (this.data.formType === 1) {
            this.setData({
                input1: 'hdsj001',
                input2: '123456',
            })
        }
    },
    getCode() {
        this.setData({
            isCodeLoading: true,
            codeText: `${ this.data.codeTime }s后重新获取`,
        })
        const timerID = setInterval(() => {
            if (this.data.codeTime === 0) {
                clearInterval(timerID)
                this.setData({
                    codeText: '获取验证码',
                    codeTime: 6,
                    isCodeLoading: false,
                })
            } else {
                this.setData({
                    codeText: `${ this.data.codeTime - 1 }s后重新获取`,
                    codeTime: this.data.codeTime - 1,

                })
            }
        }, 1000)
    },
    trim() {
        this.setData({
            input1: this.data.input1.replaceAll(' ', ''),
        })
    },
    tapViewBtn() {
        this.setData({
            isView: !this.data.isView,
        })
    },
    tapCloseBtn() {
        this.setData({
            input2: '',
        })
    },
    async submit() {
        const valid = this.validate(['input1', 'input2'])

        if (this.data.formType === 0) {
            this.showWarnMessage('接口暂时不支持手机登录')
        } else if (this.data.formType === 1 && valid) {
            const res = await login({
                account: this.data.input1,
                password: this.data.input2
            })
            if (res.code === 200) getApp().setToken(res.data)
        }
    },
    validate(data) {
        let pass = true

        if (Array.isArray(data)) {
            data.forEach(e => {
                validateItemInput.call(this, e)
            })
        } else {
            validateItemInput.call(this, data)
        }

        function validateItemInput(data) {
            this.trim()
            const prop = typeof data === 'string' ? data : data.target.dataset.prop
            const rule1 = this.data.formType === 0 ? this.data.rules1[prop][0] : this.data.rules2[prop][0]
            const rule2 = this.data.formType === 0 ? this.data.rules1[prop][1] : this.data.rules2[prop][1]

            if (!!this.data[prop] || !rule1.required) {
                pass = true
                this.setData({ message1: prop === 'input1' ? '' : this.data.message1 })
            } else {
                this.setData({
                    valid: false,
                    message1: prop === 'input1' ? rule1.message : this.data.message1,
                    message2: prop === 'input2' ? rule1.message : this.data.message2,
                })
                pass = false
            }

            if (rule2 && rule2.pattern) {
                if (new RegExp(rule2.pattern).test(this.data[prop])) {
                    pass = true
                    this.setData({ message2: prop === 'input2' ? '' : this.data.message2 })
                } else {
                    this.setData({
                        valid: false,
                        message1: prop === 'input1' ? pass ? rule2.message : this.data.message1 : this.data.message1,
                        message2: prop === 'input2' ? pass ? rule2.message : this.data.message2 : this.data.message2,
                    })
                    pass = false
                }
            }

        }

        return !this.data.message1 && !this.data.message2
    },
    showWarnMessage(msg) {
        Message.warning({
            context: this,
            offset: [80, 32],
            duration: 3000,
            content: msg,
        })
    },
})
